package com.ck.controller;

import com.ck.util.QiwuApiUtil;
import com.ck.util.R;
import com.ck.util.RecordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *
 * @author Administrator
 */

@Slf4j
@RestController
public class EoyController {

    @PostMapping("eoy/record/upload")
    @ResponseBody
    public Object uploadRecord(@RequestParam Map<String, String> params) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String panFu = "d://tmp//";
            String fileName = RecordUtil.decoderBase64File(params.get("upfile_b64"),panFu);
            QiwuApiUtil qiwuApiUtil = new QiwuApiUtil();
            String msg = qiwuApiUtil.voiceIdentify(panFu + fileName);
            resultMap.put("msg",msg);
            resultMap.put("status", 200);
            log.info(resultMap.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @PostMapping("eoy/record/upload/file")
    @ResponseBody
    public R uploadRecordFile(@RequestParam("upfile") MultipartFile file) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String panFu = "d://tmp//";
            String fileName = UUID.randomUUID()+ file.getOriginalFilename();
            if(!fileName.isEmpty()){
                byte [] bytes = file.getBytes();
                BufferedOutputStream bufferedOutputStream = new
                        BufferedOutputStream(new FileOutputStream(panFu+fileName));
                bufferedOutputStream.write(bytes);
                bufferedOutputStream.close();
            }
            QiwuApiUtil qiwuApiUtil = new QiwuApiUtil();
            String msg = qiwuApiUtil.voiceIdentify(panFu + fileName);
            resultMap.put("msg",msg);
            resultMap.put("status", 200);
            log.info(resultMap.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.OK(resultMap);
    }
}
